package com.sneakxy.actions.code;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

/**
 * 参数
 * @author 潜行的青衣
 * @see
 */
public class Argument {

	private String name;
	
	private boolean array;
	
	private ClassType type;
	
	private Set<Annotation> annotations = new LinkedHashSet<>();

	public Argument(ClassType type, String name) {
		this.type = type;
		this.name = name;
		if(type == null) {
			throw new NullPointerException("type could not be null.");
		}
		if(StringUtils.isBlank(name)) {
			throw new NullPointerException("name could not be blank.");
		}
	}
	
	public Argument(ClassType type, String name, Annotation annotation) {
		this(type, name);
		this.annotations.add(annotation);
	}
	
	public List<Type> getImportClassType() {
		List<Type> result = new ArrayList<>();
		result.add(type);
		for(Annotation annotation: annotations) {
			result.add(annotation);
		}
		return result;
	}
	
	public String toArgumentString() {
		StringBuilder s = new StringBuilder();
		boolean isMultiLine = annotations.size() > 1;
		if(isMultiLine) {
			s.append("\n\t\t\t\t");
		}
		for(Annotation annotation: annotations) {
			s.append(annotation.toString(true)).append(" ");
			if(isMultiLine) {
				s.append("\n\t\t\t\t");
			}
		}
		s.append(type.toString(true)).append(isArray() ? "[] " : " ").append(this.getName());
		return s.toString();
	}
	
	public String getName() {
		return name;
	}

	public ClassType getType() {
		return type;
	}

	public boolean isArray() {
		return array;
	}

	public Argument setArray(boolean array) {
		this.array = array;
		return this;
	}

	public Set<Annotation> getAnnotations() {
		return annotations;
	}

	public Annotation addAnnotation(Annotation annotation) {
		this.annotations.add(annotation);
		return annotation;
	}
	
}
